package com.hmy.finance.dal.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmy.finance.dal.po.ContractEarliestRentalPo;
import com.hmy.finance.dal.po.DeviceRentalRecordPo;
import com.hmy.finance.dal.po.DeviceRentalRecordQueryPo;
import com.hmy.finance.dal.po.DeviceRentalRecordUpdatePo;
import com.hmy.finance.dal.po.result.OverdueAmountResultPo;
import com.hmy.finance.job.core.rdto.dailyfee.WaitHandleContractRdto;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author songkf
 * @description 针对表【device_rental_record(设备租赁记录表)】的数据库操作Mapper
 * @createDate 2024-02-21 18:55:01
 * @Entity com.hmy.finance.dal.po.DeviceRentalRecordPo
 */
public interface DeviceRentalRecordMapper extends BaseMapper<DeviceRentalRecordPo> {

    int insertBatch(@Param("deviceList") List<DeviceRentalRecordPo> deviceRentalRecordPoList);

    List<Long> selectAllExitedEntryFormIdList(@Param("entryFormIdList") List<Long> entryFormIdList);

    /**
     * 查询所有在租设备及今日退租的设备的合同信息
     *
     * @return List<ContractExtensionInfoJson>
     * @author songkf
     */
    List<String> queryAllRentingDeviceContract(@Param("startTime") LocalDateTime localDateTime, @Param("endTime") LocalDateTime dateTime, @Param("contractId") Long contractId);

    /**
     * 根据合同ID，查询所有在租设备及今日退租的设备租赁记录
     *
     * @param contractId 合同ID
     * @return List<DeviceRentalRecordPo>
     * @author songkf
     */
    List<DeviceRentalRecordPo> queryRentingDeviceRentalRecordByContractId(@Param("contractId") Long contractId, @Param("handleDate") LocalDate handleDate);

    List<DeviceRentalRecordPo> selectDeviceByDeviceNoAndContractNo(@Param("list") List<DeviceRentalRecordPo> list);

    List<DeviceRentalRecordPo> selectRecordByCustomerIdAndSignatoryId(@Param("customerId") Long customerId, @Param("signatoryId") Long signatoryId);

    /**
     * 批量更新设备租赁费用
     *
     * @param deviceRentalRecordPoList 设备租赁记录集合
     * @return int 更新成功的数量
     * @author songkf
     */
    int updateDeviceRentalRecordFeeAmount(@Param("deviceList") List<DeviceRentalRecordPo> deviceRentalRecordPoList);

    Page<DeviceRentalRecordPo> pageQuery(@Param("page") Page<DeviceRentalRecordPo> page, @Param("list") List<DeviceRentalRecordQueryPo> list);

    List<DeviceRentalRecordPo> selectListByContractIdAndDeviceId(@Param("list") List<DeviceRentalRecordQueryPo> list);

    /**
     * 批量设置设备退租
     *
     * @param deviceRentalRecordPoList 退租设备集合
     * @return int 更新成功的数量
     * @author songkf
     */
    int batchSetRentingDevicesExited(@Param("deviceList") List<DeviceRentalRecordPo> deviceRentalRecordPoList);

    /**
     * 周期型账单更新租赁设备的对账金额和最后一次对账时间
     *
     * @param list 设备租赁记录集合
     */
    int updateReconciledAmountAndLastReconciliationTimeByPeriod(@Param("list") List<DeviceRentalRecordUpdatePo> list);

    /**
     * 总账单更新租赁设备的对账金额
     */
    int updateReconciledAmountAndLastReconciliationTimeByHistory(@Param("list") List<DeviceRentalRecordUpdatePo> list);

    /**
     * 查询指定设备在指定时间范围内的费用信息
     *
     * @param deviceId 设备ID
     * @param start    开始日期
     * @param end      结束日期
     * @return 设备费用信息
     * @author songkf
     */
    OverdueAmountResultPo selectDeviceOverdueAmount(@Param("deviceId") Long deviceId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);

    /**
     * Updates the end time for a batch of expense cycles.
     *
     * @param deviceRentalRecordPoList the list of DeviceRentalRecordPo objects to be updated
     * @return the number of expense cycles whose end time was successfully updated
     */
    int batchUpdateExpenseCycleEndTime(@Param("list") List<DeviceRentalRecordPo> deviceRentalRecordPoList);

    /**
     * 根据合同id查询已对账金额(期结汇总)
     */
    BigDecimal selectTotalReconciledAmountByContractId(@Param("contractId") Long contractId);

    /**
     * 根据合同id查询已对账金额(历史总对账比大小金额)
     */
    BigDecimal selectHistoryReconciledAmountByContractId(@Param("contractId") Long contractId);


    /**
     * Updates the stop time for a batch of device rental records.
     *
     * @param deviceRentalRecordPoList the list of DeviceRentalRecordPo objects to be updated
     * @return the number of device rental records whose stop time was successfully updated
     */
    int batchUpdateDeviceRentalRecordStop(List<DeviceRentalRecordPo> deviceRentalRecordPoList);

    List<ContractEarliestRentalPo> selectEarliestRentalRecordsByContractIds(@Param("list") List<Long> contractIds);


    List<WaitHandleContractRdto> selectWaitHandleContractInfoList(@Param("startTime") LocalDateTime startTime, @Param("contractId") Long contractId);


}




